﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using SH.STD.DataProvider;
using System.Web.Security;
using System.Web.SessionState;

namespace SH.STD.Web.Handler
{
    /// <summary>
    /// Summary description for Login
    /// </summary>
    public class Login : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string name = System.Web.HttpContext.Current.Request["name"].ToString();
            string psd = System.Web.HttpContext.Current.Request["psd"].ToString();
            if (!DataAccess.IsUserNameExist(name))
            {
                context.Response.Write(1);
            }
            else
            {
                var userInfo = DataAccess.GetUserInfo(name, psd);
                if (userInfo == null)
                {
                    context.Response.Write(2);
                }
                else
                {
                    Cmn.Session.SetUserID(userInfo.UserID.ToString());
                    FormsAuthentication.RedirectFromLoginPage(userInfo.UserName, true);
                    context.Response.Write(0);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}